// ============================== 导入依赖 ============================== //

import CORE from '@/CORE'
import Client from '@api/user/api/GroupApi'

import ComClass from '@api/ComQuery'
import ListClass from '@api/user/model/GroupListQueryReq'
import OneClass from '@api/user/model/GroupReq'
import AddClass from '@api/user/model/GroupOperateReq'
import BaseClass from '@api/user/model/GroupDto'
import AreaClass from '@api/user/model/GroupAreaDto'
import UserClass from '@api/user/model/GroupUserDto'

const EditClass = AddClass
const DelClass = OneClass

const CLIENT = CORE.grpcCLIENT(Client)
const COM = [ComClass, 'comQuery']
const AREA_LIST = [AreaClass, 'areaContent', 1]
const USER_LIST = [UserClass, 'userContent', 1]
const BASE = [BaseClass, 'group', null, (dto, obj) => CORE.grpcDTO(dto, obj, AREA_LIST, USER_LIST)]

// ============================== 导出远程接口 ============================== //

export default {
  // —————————————————————————————— 列表 —————————————————————————————— //

  async getList(param, TIP = '查询列表') {
    const DTO = CORE.grpcDTO(ListClass, param, COM, [BaseClass, 'content'])
    const resp = await CORE.grpc(TIP, CLIENT, 'queryGroupList', DTO)
    return [resp.comQuery.totalCount, resp.content || []]
  },

  // —————————————————————————————— 单项 —————————————————————————————— //

  async getOne({ id }, TIP = '查询详情') {
    const DTO = CORE.grpcDTO(OneClass, { ids: [id] })
    const resp = await CORE.grpc(TIP, CLIENT, 'queryGroupDetail', DTO)
    return resp.group || {}
  },
  transOne(obj) {
    obj.areaId = obj.areaContent.map((item) => item.areaId)
    return obj
  },

  // —————————————————————————————— 增 —————————————————————————————— //

  async add(one, TIP = '新增') {
    const DTO = CORE.grpcDTO(AddClass, one, BASE)
    await CORE.grpc(TIP, CLIENT, 'insertGroup', DTO)
    return CORE.grpcOK(TIP)
  },

  // —————————————————————————————— 改 —————————————————————————————— //

  async edit(one, TIP = '修改') {
    const DTO = CORE.grpcDTO(EditClass, one, BASE)
    await CORE.grpc(TIP, CLIENT, 'updateGroup', DTO)
    return CORE.grpcOK(TIP)
  },

  // —————————————————————————————— 删 —————————————————————————————— //

  async del({ id }, TIP = '删除') {
    const DTO = CORE.grpcDTO(DelClass, { ids: [id] })
    await CORE.grpc(TIP, CLIENT, 'deleteGroup', DTO)
    return CORE.grpcOK(TIP)
  },
}
